package com.sjy.imagepicker.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.request.RequestOptions;
import com.sjy.imagepicker.R;
import com.sjy.imagepicker.utils.Utils;

import java.io.File;
import java.util.List;

/**
 * Created by zhang on 2016/6/29.
 */
public class ImageAdapter extends BaseAdapter {

    private List<String> mPickedImgs;
    private static final String TAG = "test";
    private List<String> mImgPaths;
    private Context context;
    private LayoutInflater inflater;
    private int gridwidth;
    private int mMaxCount;

    public ImageAdapter( Context context, List<String> pickedImgs,int maxCount) {

        this.mPickedImgs = pickedImgs;
        this.context = context;
        this.mMaxCount=maxCount;
        inflater= LayoutInflater.from(context);
        int pxOf3dp= Utils.dp2px(context,3);
        int width = context.getResources().getDisplayMetrics().widthPixels;
        gridwidth= (int) ((width-pxOf3dp*2)/3+0.5);
    }

    public void setImgPaths(List<String> imgPaths){
        this.mImgPaths = imgPaths;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {

        return mImgPaths==null?0:mImgPaths.size();
    }

    @Override
    public String getItem(int position) {
        return mImgPaths.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView=inflater.inflate(R.layout.gv_item,parent,false);
            AbsListView.LayoutParams lps=new AbsListView.LayoutParams(gridwidth,gridwidth);
            convertView.setLayoutParams(lps);
            viewHolder=new ViewHolder();
            viewHolder.iv= (ImageView) convertView.findViewById(R.id.item_iv);
            viewHolder.ibSelect= (ImageView) convertView.findViewById(R.id.item_iv_select);
            convertView.setTag(viewHolder);

        }
        else{
            viewHolder= (ViewHolder) convertView.getTag();
        }



        final String imagePath = getItem(position);

        viewHolder.ibSelect.setImageResource(R.drawable.ic_uncheck);
        viewHolder.ibSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!new File(imagePath).exists()) {
                    Log.d(TAG, "onClick: 文件不存在");
                }

                if (mPickedImgs.contains(imagePath)) {
                    mPickedImgs.remove(imagePath);
                    viewHolder.ibSelect.setImageResource(R.drawable.ic_uncheck);
                    viewHolder.iv.setColorFilter(null);
                }else {
                    if (mPickedImgs.size()>=mMaxCount){
                        Toast.makeText(context, "最多只能选择"+mMaxCount+"张", Toast.LENGTH_SHORT).show();
                        return ;
                    }
                    mPickedImgs.add(imagePath);
                    viewHolder.ibSelect.setImageResource(R.drawable.ic_check);
                    viewHolder.iv.setColorFilter(Color.parseColor("#77000000"));
                }
                if (onImagePickedListener != null) {
                    onImagePickedListener.onImagePicked();
                }
            }
        });

        viewHolder.iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(position);
                }
            }
        });

        RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.ic_no)
                .error(R.drawable.ic_no)
                .centerCrop()
                .priority(Priority.HIGH);

        Glide.with(context)
                .load(new File(imagePath))
                .apply(options)
                .into(viewHolder.iv);

        if (!mPickedImgs.contains(imagePath)) {
            viewHolder.ibSelect.setImageResource(R.drawable.ic_uncheck);
            viewHolder.iv.setColorFilter(null);
        }else {
            viewHolder.ibSelect.setImageResource(R.drawable.ic_check);
            viewHolder.iv.setColorFilter(Color.parseColor("#77000000"));
        }


        return convertView;
    }
    static class ViewHolder{
        ImageView iv;
        ImageView ibSelect;
    }
    public interface OnImagePickedListener{
        public void onImagePicked();
    }
    private OnImagePickedListener onImagePickedListener;

    public void setOnImagePickedListener(OnImagePickedListener onImagePickedListener) {
        this.onImagePickedListener = onImagePickedListener;
    }

    public interface OnItemClickListener{
        public void onItemClick(int position);
    }
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}

